package com.codingvip.concurrent;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author hansongtao
 */
public class DelayedElement implements Delayed {
    private String name;
    private long time;

    // Contructor of DelayObject
    public DelayedElement(String name, long delayTime) {
        this.name = name;
        this.time = System.currentTimeMillis()
                + delayTime;
    }



    // Implementing getDelay() method of Delayed
    @Override
    public long getDelay(TimeUnit unit) {
        long diff = time - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }

    // Implementing compareTo() method of Delayed
    @Override
    public int compareTo(Delayed obj) {
        if (this.time < ((DelayedElement) obj).time) {
            return -1;
        }
        if (this.time > ((DelayedElement) obj).time) {
            return 1;
        }
        return 0;
    }

    // Implementing toString() method of Delayed
    @Override
    public String toString() {
        return "\n{"
                + "name=" + name
                + ", time=" + time
                + "}";
    }
}
